//
//  ERCountryListViewController.m
//  ExchangeRate
//
//  Created by lanou on 15/11/7.
//  Copyright © 2015年 方义强. All rights reserved.
//

#import "ERCountryListViewController.h"
#import "ERCountryListTableViewCell.h"
#import "UITableView+Wave.h"
#define kCell @"ListCell"
@interface ERCountryListViewController ()<UITableViewDataSource, BATableViewDelegate>
@property (nonatomic, strong) UIScrollView *mainScrollView;
@end

@implementation ERCountryListViewController

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeSuperView" object:@"aa" userInfo:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    // topView 初始化
    self.topView.backgroundColor = [UIColor clearColor];
    _lineView.backgroundColor = [UIColor grayColor];
    self.titleLabel.text = NSLocalizedString(@"切换汇率", nil);
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.leftButton setTitle:NSLocalizedString(@"关闭", nil) forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(clickCloseBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    // scrollView 初始化
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, kSCREENWIDTH, kSCREENHEIGHT - 70)];
    _mainScrollView.backgroundColor = [UIColor clearColor];
    _mainScrollView.contentSize = CGSizeMake(kSCREENWIDTH * 2, kSCREENHEIGHT - 70);
    _mainScrollView.contentOffset = CGPointMake(kSCREENWIDTH, 0);
    _mainScrollView.bounces = NO;
    _mainScrollView.delegate = self;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.userInteractionEnabled = YES;
    _mainScrollView.pagingEnabled = YES;
    [self.view addSubview:_mainScrollView];
    
    [self initForTableView];
    [self openDataBase];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView的初始化
- (void) initForTableView
{
    _listTV = [[BATableView alloc] initWithFrame:CGRectMake(kSCREENWIDTH, 0, kSCREENWIDTH, kSCREENHEIGHT - 70)];
    _listTV.backgroundColor = [UIColor clearColor];
    _listTV.tableView.backgroundColor = [UIColor clearColor];
    _listTV.delegate = self;
    _listTV.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_listTV.tableView registerClass:[ERCountryListTableViewCell class] forCellReuseIdentifier:kCell];
    [_mainScrollView addSubview:_listTV];
}

#pragma mark 返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataTitleArr.count;
}

#pragma mark 返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataCellArr[section] count];
}

#pragma mark 设置右边栏
- (NSArray *) sectionIndexTitlesForABELTableView:(BATableView *)tableView {
    return _dataTitleArr;
}

#pragma mark 设置每组的开头
- (NSString *)titleString:(NSInteger)section {
    if (section == 0) {
        return NSLocalizedString(@"贵重金属", nil);
    }
    return [NSString stringWithFormat:@"%@", _dataTitleArr[section]];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return NSLocalizedString(@"贵重金属", nil);
    }
    return [NSString stringWithFormat:@"%@", _dataTitleArr[section]];
}

#pragma mark cell 重用 与 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ERCountryListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    cell.configureModel = self.dataCellArr[indexPath.section][indexPath.row];
    NSString *str = [[self.nameDic[self.dataTitleArr[indexPath.section]][indexPath.row] stringByAppendingString:@"  "] stringByAppendingString:self.simDic[self.dataTitleArr[indexPath.section]][indexPath.row]];
    cell.nameStr = str;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark 返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark 返回分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, kSCREENWIDTH - 40, 20)];
    sectionView.backgroundColor = [UIColor colorWithRed:0.072 green:0.075 blue:0.075 alpha:1.000];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor lightGrayColor];
    [sectionView addSubview:label];
    label.text = _dataTitleArr[section];
    if (section == 0) {
         label.text = NSLocalizedString(@"贵重金属", nil);
    }
    return sectionView;
}

#pragma mark cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ERResourceModel *model = [self selectedModel:self.simDic[self.dataTitleArr[indexPath.section]][indexPath.row]];
    model.isEdit = 0;
    model.fields.isSelected = 1;
    [self.dataBase executeUpdate:@"update ERData set isSelected = '0' where name = ?", _editName];
    [self.dataBase executeUpdate:@"update ERData set isSelected = '1' where name = ?", model.fields.name];
    [self.dataBase executeUpdate:@"delete from ERShowData where name = ?", _editName];
    [self.dataBase executeUpdate:@"insert into ERShowData values( ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.fields.name, model.fields.price, model.fields.symbol, model.fields.ts, model.fields.type, model.fields.utctime, model.fields.volume, [NSString stringWithFormat:@"%d", model.fields.isSelected], [NSString stringWithFormat:@"%d", model.fields.isLocation]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"selected" object:@"list" userInfo:[NSDictionary dictionaryWithObject:model forKey:@"model"]];
    [self viewDisMissFromSuperView];
}

#pragma mark 返回选中的model类
- (ERResourceModel *) selectedModel:(NSString *)simName
{
    for ( NSArray *eveArr in _dataCellArr) {
        for (ERResourceModel *model in eveArr) {
            if ([model.fields.symbol containsString:simName]) {
                return model;
            }
        }
    }
    return 0;
}

#pragma mark uiscrollerView 向右滑动消失页面
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _mainScrollView) {
        if (scrollView.contentOffset.x < kSCREENWIDTH / 2) {
            [self viewDisMissFromSuperView];
        }
    }
}

#pragma mark 页面消失
- (void) viewDisMissFromSuperView
{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.3;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"push";
    animation.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:animation forKey:nil];
    [self.view removeFromSuperview];
}

#pragma mark 点击关闭按钮
- (void) clickCloseBtn:(UIButton *)btn
{
    [self viewDisMissFromSuperView];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
